package view.displayManagers;

import java.awt.Color;
import java.awt.Point;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import model.Board;
import controller.HumanControlledGame;

import utils.ActivateButton;
import view.DisplayManager;
import view.GameHazardsDisplay;
import view.actionListeners.SinglePlayerListener;

/**
 * Class implements a display manager for automatic game mode
 */
public class SinglePlayerDisplayTool extends GameDisplayTool
		implements SingplePlayerDisplayMngr, DisplayManager {

	// members
	private HumanControlledGame gameManager; // game manager
	private InputOutputMngr ioManager; // manages notifications for the user
	private ClickedPiece lastToBeClicked; // holds data about the last-to-be-clicked button 
	private ActivateButton buttonActivator; // used to switch board buttons on or off
	private GameHazardsDisplay hazardsDisplayTool; // used to display // TODO object name
	
	/**
	 * Constructor- creates a new display tool for single player game
	 * @param iconMap- button icons
	 * @param light- light button color
	 * @param dark- dark button color
	 * @param boardSize- game board size
	 */
	public SinglePlayerDisplayTool(Map<String, ImageIcon> iconMap, Color light, Color dark, int boardSize, HumanControlledGame gameManager, List<JButton> boardButtons, Board gameBoard, GameHazardsDisplay hazardsDisplayTool) {
		
		// initialize members using base class constructor
		super(iconMap, light, dark, boardSize, boardButtons, gameBoard);
		
		// set game controller
		this.gameManager = gameManager;
		
		// allocate input-output manager
		this.ioManager = new ContactUser();
		
		// allocate last-to-be clicked button
		lastToBeClicked = new ClickedPiece(new Point());
		
		// allocate button activator
		buttonActivator = new ActivateButton();
		// set system as activate
		buttonActivator.activateSystem(); // TODO check if should be disabled first
		
		this.hazardsDisplayTool = hazardsDisplayTool;
	}
	
	/**
	 * Pairs board buttons with listener for single player game
	 */
	@Override
	public void setButtons(List<JButton> boardButtons, Color mark) {
		
		// create a new listener
		SinglePlayerListener listener = new SinglePlayerListener(super.getBoardSize(), mark, this, gameManager, lastToBeClicked, boardButtons, buttonActivator);
		
		// scan board buttons list and add listener to each button
		for (JButton currButton : boardButtons) {
			currButton.addActionListener(listener);
		} 
	}

	/**
	 * Sets the board with two-players layout
	 */
	@Override
	public void setBoard() {
		// set board
		gameManager.setTowPlayersBoard();
	}
	
	/**
	 * Uses base class's method to refresh the board display, in order
	 * to update the changes
	 */
	@Override
	public void refreshBoardDisplay() {
		super.refreshBoardDisplay(lastToBeClicked);
	}
	
	/**
	 * Notify user that game is in Check
	 */
	@Override
	public void notifyCheck() {
		hazardsDisplayTool.displayCheck();
	}

	/**
	 * Notify user that game is over and display given message
	 */
	@Override
	public void gameOver(String msg) {
		hazardsDisplayTool.gameOver(msg);
	}
	
	/**
	 * Display options for pieces to replace the Pawn,
	 * and return user's choice
	 */
	@Override
	public int replacePawn(String[] options, int defualtOption) {
		return ioManager.notifyReplacePawn(options, defualtOption);
	}
}